package com.techbrand.lb7.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.techbrand.lb7.domain.Address;

public class AddressValidator implements Validator {

	private ZipCodeValidator zipCodeValidator;
	
	public ZipCodeValidator getZipCodeValidator() {
		return zipCodeValidator;
	}

	public void setZipCodeValidator(ZipCodeValidator zipCodeValidator) {
		this.zipCodeValidator = zipCodeValidator;
	}

	@SuppressWarnings("rawtypes")
	public boolean supports(Class clazz) {
		return Address.class.isAssignableFrom(clazz);
	}
	
	public void validate(Object obj, Errors errors) {
		Address address = (Address) obj;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "city", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "province", "required");
		
		ValidationUtils.invokeValidator(zipCodeValidator, address.getZipCode(), errors);
	}
}
